package com.netintech.intelligenceInterface.common.interceptor;

import com.netintech.intelligenceInterface.common.customException.InterfaceCallException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;

import static com.netintech.intelligenceInterface.common.Type.ERROR;

/**
 * @author lining
 * @date 2021/5/18 14:36
 * 演示接口验证
 * 存在演示接口，演示接口时对外暴露的。
 * 为了防止演示接口，被非法调用，需要进行验证。
 * 验证原理： 演示接口调用IP白名单。只有白名单内的IP才会允许访问
 */
public class InterfaceDemonstrationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String remoteHost = request.getRemoteHost();
        //白名单验证 白名单从数据库或缓存中获取
        List<String> list = new ArrayList<>();
        list.add("172.16.11.211");
        for (String ip : list) {
            if (ip.contains(remoteHost)) {
                return true;
            }
        }
        throw new InterfaceCallException(ERROR, "接口非法调用！");
    }
}
